package com.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.item.bean.Users;
import com.item.domain.TUsers;
import com.item.mapper.TUsersMapper;
import com.item.po.TUsersPo;
import com.item.service.TUsersService;
import com.item.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TUsersServiceImpl implements TUsersService {

    @Autowired
    TUsersMapper tUsersMapper;

    public List<TUsersPo> findAll(int limit, int page, TUsers tUsers) {
        if (limit > 0 && page > 0) {
            PageHelper.startPage(limit, page);
        }
        return tUsersMapper.findAll(tUsers);
    }


    public int add(TUsers tUsers) {

        int i = 0;
        try {
            tUsers.setCreateTime(System.currentTimeMillis());
            //加密
            String s = MD5Utils.md5(tUsers.getPassWord());
            tUsers.setPassWord(s);
            i = tUsersMapper.insertSelective(tUsers);
        } catch (Exception e) {
            return i;
        }
        return i;
    }


    public int update(TUsers tUsers) {
        int i = 0;
        try {
            tUsers.setUpdateTime(System.currentTimeMillis());
            //加密
            //String s = MD5Utils.md5(tUsers.getPassWord());
            //tUsers.setPassWord(s);
            i = tUsersMapper.updateByPrimaryKeySelective(tUsers);
        } catch (Exception e) {
            return i;
        }
        return i;

    }

    @Override
    public int delete(int id) {
        int i = 0;
        try {
            i = tUsersMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            return i;
        }
        return i;
    }

    @Override
    public int userInfoAddMobile(TUsers users) {
        int i = 0;
        try {
            i = tUsersMapper.insertSelective(users);
        } catch (Exception e) {
            return 0;
        }
        return 0;
    }

    @Override
    public TUsers findAllUsers(String phone) {
        TUsers users = tUsersMapper.selectByPrimaryKeyPhone(phone);
        if (users == null) {
            TUsers users1 = new TUsers();
            users1.setTelePhone(phone);
            int i = tUsersMapper.insertSelective(users1);
            if (i == 1) {
                users = tUsersMapper.selectByPrimaryKeyPhone(phone);
            }
        }
        return users;
    }

    @Override
    public int userInfoAddMobileUpdate(TUsers users) {
        int i = 0;
        try {
            i = tUsersMapper.updateByPrimaryKeySelective(users);
        } catch (Exception e) {
            return 0;
        }
        return i;
    }


}
